package com.inossem.warehousestorage_inossem.adapter.refundStorage;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.bean.refundStorage.RefundStorageSaleBatchListBean;
import com.inossem.warehousestorage_inossem.utils.RefundStorageUtils;
import com.zhy.android.percent.support.PercentLinearLayout;

import java.util.List;

/**
 * Created by leij on 2017/6/6.
 */

public class RefundStorageSaleBatchListAdapter extends BaseAdapter {

    private Context mContext;
    private List<RefundStorageSaleBatchListBean.SaleBatchBodyBean> mBatchList;
    private LayoutInflater inflater;
    private BatchListAdapterListener mBatchListAdapterLister;

    public RefundStorageSaleBatchListAdapter(Context context, List<RefundStorageSaleBatchListBean.SaleBatchBodyBean> batchList, BatchListAdapterListener batchListAdapterLister) {
        mContext = context;
        mBatchList = batchList;
        mBatchListAdapterLister = batchListAdapterLister;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mBatchList.size();
    }

    @Override
    public Object getItem(int position) {
        return mBatchList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        RefundStorageSaleBatchListBean.SaleBatchBodyBean BatchItem = mBatchList.get(position);
        ViewHolder viewHolder;
        if (convertView == null || (convertView != null && convertView.getTag() == null)) {
            convertView = inflater.inflate(R.layout.view_refund_storage_select, null);
            viewHolder = new ViewHolder();
            //初始化控件
            initView(viewHolder, convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        //初始化数据
        initData(mContext, viewHolder, convertView, BatchItem);
        return convertView;
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.text = (TextView) view.findViewById(R.id.text);
        viewHolder.value = (TextView) view.findViewById(R.id.value);
        viewHolder.batchLayout = (PercentLinearLayout) view.findViewById(R.id.batchLayout);
    }

    private void initData(Context context, ViewHolder viewHolder, View view, RefundStorageSaleBatchListBean.SaleBatchBodyBean batchItem) {
        viewHolder.text.setText(batchItem.getBatch());
        List<RefundStorageSaleBatchListBean.SaleBatchBodyBean.BatchSpecListBean> batchInfoList = batchItem.getBatch_spec_list();
        if (batchInfoList != null && !batchInfoList.isEmpty()) {
            viewHolder.batchLayout.removeAllViews();
            for (int i = 0; i < batchInfoList.size(); i++) {
                RefundStorageSaleBatchListBean.SaleBatchBodyBean.BatchSpecListBean batchInfo = batchInfoList.get(i);
                RefundStorageUtils.createTextView(context, viewHolder.batchLayout, batchInfo.getBatch_spec_name(), batchInfo.getBatch_spec_value(), false);
            }
        }
        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                mBatchListAdapterLister.onSelect(batchItem.getBatch(), batchItem.getBatch_spec_list());
            }
        });
    }

    private class ViewHolder {
        private TextView text, value;

        private PercentLinearLayout batchLayout;
    }

    public static abstract class BatchListAdapterListener {
        public abstract void onSelect(String batchNumber, List<RefundStorageSaleBatchListBean.SaleBatchBodyBean.BatchSpecListBean> batchInfoList);
    }
}
